<template>
  <div class="login-page">
    <el-card class="box-card">
      <template #header>
        <span>黑马面经运营后台</span>
      </template>
      <!-- 表单区域 -->
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item class="tc">
          <el-button type="primary" @click="onSubmit">登录</el-button>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'HmmhPc03Index',

  data() {
    return {
      form: {
        username: 'admin',
        password: 'admin'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { pattern: /^\w{5,12}$/, message: '长度在5 到 12个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^\w{5,12}$/, message: '长度在5 到 12个数字', trigger: 'blur' }
        ]
      }
    }
  },

  mounted() {},

  methods: {
    // 点击按钮去调用dispatch触发vuex的actions
    async onSubmit() {
      try {
        // 兜底校验
        await this.$refs.form.validate()
        // 发请求,在vuex中发请求
        await this.$store.dispatch('user/loginArticle', this.form)
        // 提示登录成功
        this.$message.success('登录成功')
        // 跳转页面
        this.$router.push('/')
      } catch (error) {}
    },
    // 重置按钮
    reset() {
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
.login-page {
  min-height: 100vh;
  background: url(@/assets/login-bg.svg) no-repeat center / cover;
  display: flex;
  align-items: center;
  justify-content: space-around;
  .el-card {
    width: 420px;
    ::v-deep .el-card__header {
      height: 80px;
      line-height: 40px;
      background-color: #737bf6;
      text-align: center;
      font-size: 18px;
      color: #fff;
    }
  }
  .el-form {
    padding: 0 20px;
  }
  .tc {
    text-align: center;
  }
}
</style>
